{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "### VarianceThreshold\n",
    "\n",
    "VarianceThreshold 是 Scikit-learn 提供的一种特征选择方法，用于移除低方差的特征。它通过计算特征的方差来判断特征的重要性，假设方差较低的特征包含的信息较少，因此可以移除。这种方法适用于无监督学习中的特征选择。\n",
    "\n",
    "主要参数：\n",
    "\n",
    "threshold: 方差阈值，默认为 0。所有方差小于或等于该阈值的特征将被移除。\n",
    "\n",
    "主要方法：\n",
    "\n",
    "it(X): 计算每个特征的方差。\n",
    "\n",
    "transform(X): 根据方差阈值筛选特征。\n",
    "\n",
    "fit_transform(X): 先拟合数据，再进行特征选择。"
   ],
   "id": "eecce9d2f3cbf47e"
  },
  {
   "cell_type": "code",
   "id": "bbe49a07311e6164",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-01-09T17:01:50.740384Z",
     "start_time": "2025-01-09T17:01:50.736577Z"
    }
   },
   "source": [
    "from sklearn.feature_selection import VarianceThreshold\n",
    "import numpy as np\n",
    "\n",
    "# 示例数据\n",
    "data = np.array([[0, 2, 0], [0, 1, 4], [0, 3, 1]])\n",
    "print(\"原始数据:\\n\", data)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数据:\n",
      " [[0 2 0]\n",
      " [0 1 4]\n",
      " [0 3 1]]\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "移除方差为 0 的特征",
   "id": "6acf2cd56e6f49d7"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-09T17:02:43.784362Z",
     "start_time": "2025-01-09T17:02:43.779785Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 创建 VarianceThreshold 对象，默认阈值为 0\n",
    "selector = VarianceThreshold()\n",
    "\n",
    "# 拟合并转换数据\n",
    "selected_data = selector.fit_transform(data)\n",
    "\n",
    "print(\"筛选后的数据:\\n\", selected_data)"
   ],
   "id": "initial_id",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "筛选后的数据:\n",
      " [[2 0]\n",
      " [1 4]\n",
      " [3 1]]\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "自定义方差阈值",
   "id": "ce383cd91e6af0fe"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-09T17:02:45.694761Z",
     "start_time": "2025-01-09T17:02:45.690508Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 创建 VarianceThreshold 对象，设置阈值为 1\n",
    "selector = VarianceThreshold(threshold=1)\n",
    "\n",
    "# 拟合并转换数据\n",
    "selected_data = selector.fit_transform(data)\n",
    "\n",
    "print(\"筛选后的数据:\\n\", selected_data)"
   ],
   "id": "161a2e55b1c05df6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "筛选后的数据:\n",
      " [[0]\n",
      " [4]\n",
      " [1]]\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "### PCA\n",
    "\n",
    "PCA（Principal Component Analysis，主成分分析） 是一种常用的降维技术，通过线性变换将高维数据映射到低维空间，同时尽可能保留数据的方差信息。PCA 广泛应用于数据压缩、可视化、噪声过滤和特征提取等领域。\n",
    "\n",
    "主要参数：\n",
    "\n",
    "n_components: 指定降维后的维度数，可以是整数（具体维度）或小数（保留的方差比例）。\n",
    "\n",
    "whiten: 是否对数据进行白化处理（使每个特征的方差为 1），默认为 False。\n",
    "\n",
    "svd_solver: 指定奇异值分解（SVD）的求解器，可选值包括 auto、full、arpack、randomized。\n",
    "\n",
    "主要方法：\n",
    "\n",
    "fit(X): 计算主成分。\n",
    "\n",
    "transform(X): 将数据投影到主成分上。\n",
    "\n",
    "fit_transform(X): 先拟合数据，再进行降维。\n",
    "\n",
    "inverse_transform(X): 将降维后的数据还原到原始空间。"
   ],
   "id": "12991659371b5a12"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-09T17:13:27.936359Z",
     "start_time": "2025-01-09T17:13:27.931959Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.decomposition import PCA\n",
    "import numpy as np\n",
    "\n",
    "# 示例数据\n",
    "data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n",
    "print(\"原始数据:\\n\", data)"
   ],
   "id": "a5afd59570eb66fa",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数据:\n",
      " [[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "将数据降维到 2 维",
   "id": "efba1c3e3821ed7f"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-09T17:14:38.012832Z",
     "start_time": "2025-01-09T17:14:37.998090Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 创建 PCA 对象，降维到 2 维\n",
    "pca = PCA(n_components=2)\n",
    "\n",
    "# 拟合并转换数据\n",
    "reduced_data = pca.fit_transform(data)\n",
    "\n",
    "print(\"降维后的数据:\\n\", reduced_data)"
   ],
   "id": "6cd98f567eb0c627",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "降维后的数据:\n",
      " [[-5.19615242e+00 -2.56395025e-16]\n",
      " [ 0.00000000e+00 -0.00000000e+00]\n",
      " [ 5.19615242e+00 -2.56395025e-16]]\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "保留 95% 的方差",
   "id": "4d015e386c883e99"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-09T17:15:54.095070Z",
     "start_time": "2025-01-09T17:15:54.089845Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 创建 PCA 对象，保留 95% 的方差\n",
    "pca = PCA(n_components=0.95)\n",
    "\n",
    "# 拟合并转换数据\n",
    "reduced_data = pca.fit_transform(data)\n",
    "\n",
    "print(\"降维后的数据:\\n\", reduced_data)"
   ],
   "id": "1881d6d99f63f913",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "降维后的数据:\n",
      " [[-5.19615242]\n",
      " [ 0.        ]\n",
      " [ 5.19615242]]\n"
     ]
    }
   ],
   "execution_count": 11
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
